﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace _09.HashedSet
{
    class Program
    {
        static StringBuilder output = new StringBuilder();
        static HashedSet<int> set = new HashedSet<int>();

        static void Main(string[] args)
        {
            //TestGenerator.CreateTest("test10.in.txt");
            //ReadInput(@"..\..\Tests\test10.in.txt");
            //PrintOutput(@"..\..\Tests\test10.out.txt");
            ReadInput();
            PrintOutput();
        }

        private static void ReadInput()
        {
            while (true)
            {
                string commandText = Console.ReadLine();
                if (commandText == "End" || commandText == null)
                {
                    break;
                }
                try
                {
                    ProcessCommand(commandText);
                }
                catch (Exception ex)
                {
                    output.Append("Unhandled exception: " + ex);
                }
            }

        }

        private static void ReadInput(string fileName)
        {
            StreamReader reader = new StreamReader(fileName);
            string line = reader.ReadLine();
            while (line != String.Empty && line != "End")
            {
                ProcessCommand(line);
                line = reader.ReadLine();
            }

        }

        private static void ProcessCommand(string commandText)
        {
            string[] command = commandText.Split(new string[] { " | " }, StringSplitOptions.RemoveEmptyEntries);
            switch (command[0])
            {
                case "Add":
                    {
                        int newElement = int.Parse(command[1]);
                        if (!set.Find(newElement))
                        {
                            set.Add(newElement);
                        }
                        break;
                    }
                case "Remove":
                    {
                        int newElement = int.Parse(command[1]);
                        set.Remove(newElement);
                        break;
                    }
                case "Find":
                    {
                        output.AppendLine(set.Find(int.Parse(command[1])).ToString());
                        break;
                    }

                case "Count":
                    {
                        output.AppendLine(set.Count.ToString());
                        break;
                    }
                case "Clear":
                    {
                        set.Clear();
                        break;
                    }
                case "Union":
                    {
                        string[] elements = command[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        HashedSet<int> newHash = new HashedSet<int>();
                        foreach (var item in elements)
                        {
                            int newElement = int.Parse(item);
                            if (!newHash.Find(newElement))
                            {
                                newHash.Add(newElement);
                            }
                        }
                        set.Union(newHash);
                        break;
                    }
                case "Intersect":
                    {
                        string[] elements = command[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        HashedSet<int> newHash = new HashedSet<int>();
                        foreach (var item in elements)
                        {
                            int newElement = int.Parse(item);
                            if (!newHash.Find(newElement))
                            {
                                newHash.Add(newElement);
                            }
                        }
                        set.Intersect(newHash);
                        break;
                    }
                case "Foreach":
                    {
                        foreach (var item in set)
                        {
                            output.Append(item + ",");
                        }
                        if (output.Length > 1)
                        {
                            output.Remove(output.Length - 1, 1);
                            output.AppendLine();
                        }
                        break;
                    }
            }
        }

        private static void PrintOutput()
        {

            Console.WriteLine(output);
        }

        private static void PrintOutput(string fileName)
        {

            StreamWriter writer = new StreamWriter(fileName);
            using (writer)
            {
                writer.Write(output);
            }
        }
    }
}
